<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\controllers;
use Yii;
/**
 * Description of GoodsController
 *
 * @author Administrator
 */
class GoodsController extends \yii\web\Controller {
    //put your code here
    //添加商品
    public function actionAdd(){
        $model = new \app\models\Goods();
        $data = Yii::$app->request->post();
        if($model->load($data)){
            if($model->validate()){
                $model->save();
            }
        }
        //$model调用表中的字段
//        $model->goods_name='苹果';
//        $model->goods_price=5;
//        $model->save();
       
        return $this->render("goods_add",['model'=>$model]);
    }
    
    //修改
    public function actionModify($goods_id) {
        $model = new \app\models\Goods();
       //Goods::find();
        $info = $model->find()->where(['goods_id'=>$goods_id])
                ->asArray()  //以数组的形式返回
                ->one();     //一条数据
        //判断是否是post请求
        if(Yii::$app->request->isPost){
            $data=Yii::$app->request->post();
//            $model->findOne($goods_id);
            $model->goods_id=$goods_id;
            $model->goods_name=$data['goods_name'];
            $model->goods_price=$data['goods_price'];
            $model->save();
            exit;
        }
        return $this->render('goods_modify',['model'=>$model,'info'=>$info]);

    }
   
    //自动生成修改功能的form表单
   public function actionEdit($id){
       $goods = \app\models\Goods::findOne($id);
//       var_dump($goods);exit;
       if(Yii::$app->request->getIsPost()){  //判断是否是post请求
           $goods->load(Yii::$app->request->Post());
           if($goods->save()){
               $this->redirect(\yii\helpers\Url::toRoute('goods/list'));
           }
       }
     return $this->render('goods_add',['model'=>$goods]);       
       
       
   } 
    
  //分页类
   public function actionList(){
       $count = (new \app\models\Goods())->find()->count();  //总数据量
       $page = new \yii\data\Pagination([
           'defaultPageSize'=>3,            //每页的数据
           'totalCount'=>$count
       ]);
       $list = (new \app\models\Goods)->find()
               ->offset($page->offset)
               ->limit($page->limit)        
               ->orderBy('goods_price')   
               ->all();
       return $this->render('index',['page'=>$page,'list'=>$list]);
       
   }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    function actionTest(){
        
       //      $info = $model->find()->where("id=$id")->one();
        //$info = $model->findOne(1);  //通过主键查询一条数据,并以对象的形式返回
        $list = $model->find()->asArray()->all();  //all()查询所有数据
//        var_dump($info);
        
        
        
     /*   //sql过滤,防止sql注入
        $post = Yii::$app->db->createCommand('SELECT * FROM t_goods WHERE goods_id=:id')
                ->bindValue(':id', 1)
                ->queryOne();
        
        $post = Yii::$app->db->createCommand('SELECT * FROM t_goods WHERE goods_id=:id')->queryOne();
        
        */
        
        //query对象中select(字段)->from(表名) query执行select操作
//        $post = (new \Yii\db\Query())->select(['goods_id','goods_name'])
//                ->from("t_goods")->where(['goods_id'=>1])->one();
//         
//        $post = (new \yii\db\Query())->select(['goods_price','goods_name'])->from('t_goods') //每一条查询都要new一个query 查询里最好不要带*
//                 ->offset(1)   //从第几条
//                 ->limit(2)    //查询几条
//                 ->all();
        //union表合并,可以结构不一样 
        
        
         var_dump($post); 
        
        
    }
    
    
    
    
    
    
    
    
    
}
